package com.example.demo.TakeMaterial.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.demo.TakeMaterial.dto.purchaseGood;
import com.example.demo.TakeMaterial.entity.MaterialRequisition;
import com.example.demo.TakeMaterial.entity.RequisitionHeader;
import com.example.demo.TakeMaterial.mapper.MaterialRequisitionMapper;
import com.example.demo.TakeMaterial.mapper.RequisitionHeaderMapper;
import com.example.demo.TakeMaterial.service.RequisitionHeaderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.common.ResponseData;
import com.example.demo.entryWarehouse.entity.AcceptGoods;
import com.example.demo.entryWarehouse.mapper.AcceptGoodsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wangxuan
 * @since 2021-05-17
 */
@Service
public class RequisitionHeaderServiceImpl extends ServiceImpl<RequisitionHeaderMapper, RequisitionHeader> implements RequisitionHeaderService {

    @Autowired
    private RequisitionHeaderMapper requisitionHeaderMapper;
    @Autowired
    private MaterialRequisitionMapper materialRequisitionMapper;

    @Autowired
    private AcceptGoodsMapper acceptGoodsMapper;

    //@Autowired
    //private
    @Override
    public ResponseData selectAll1(String department) {

        QueryWrapper<RequisitionHeader> wrapper = new QueryWrapper<>();
        wrapper.eq("department",department);
        List<RequisitionHeader> requisitionHeader = requisitionHeaderMapper.selectList(wrapper);
        //System.out.println(wrapper);
       // System.out.println(requisitionHeader);
        return ResponseData.buildOk(requisitionHeader);
    }

    @Override
    public ResponseData selectAll() {
        List<RequisitionHeader> requisitionHeader = requisitionHeaderMapper.selectList(null);
        return ResponseData.buildOk(requisitionHeader);
    }

    @Override
    public ResponseData selectByNot() {
        HashMap<String,Object> map = new HashMap<>();
        map.put("status","申请中");
        List<RequisitionHeader> acceptInformList = requisitionHeaderMapper.selectByMap(map);
        return ResponseData.buildOk(acceptInformList);
    }

    @Override
    public ResponseData AddPurchase(purchaseGood Goods) {

        System.out.println(Goods);
        String firstdate = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMM")) + "01000000";
        String nowdate = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));

        QueryWrapper<RequisitionHeader> wrapper = new QueryWrapper<>();

        QueryWrapper<RequisitionHeader> wrapper1 = new QueryWrapper<>();

        wrapper1.eq("provider",Goods.getProvider())
                .eq("department",Goods.getDepartment())
                .eq("accept_id",Goods.getAccept_id());

        int m = requisitionHeaderMapper.selectCount(wrapper1);
            System.out.println(m);
            if(m > 0){
                return ResponseData.buildError();
            }
        wrapper.between("createtime",(Object) firstdate,(Object)nowdate);

        Integer num = requisitionHeaderMapper.selectCount(wrapper);

        //领料流水号
        String requisitionId = LocalDate.now().getYear() + "-" +LocalDate.now().getMonthValue();


        //自动生成当前的流水号
        if (num == 0){
            requisitionId = requisitionId +"-1";
        }else{
            requisitionId = requisitionId + "-" + Integer.toString(num+1);
        }
        System.out.println(requisitionId);
        RequisitionHeader requisitionHeader = new RequisitionHeader();
        //实体类信息绑定
        //领料流水号
        requisitionHeader.setRequisitionId(requisitionId);
        //验收流水号
        requisitionHeader.setAcceptId(Goods.getAccept_id());

        //部门名称
        requisitionHeader.setDepartment(Goods.getDepartment());
        //供货商
        requisitionHeader.setProvider(Goods.getProvider());
        //申请状态
        requisitionHeader.setStatus("申请中");
        //创建时间
        requisitionHeader.setCreatetime(LocalDateTime.now());
        //数据插入
        int i = requisitionHeaderMapper.insert(requisitionHeader);
        //有影响行数，则返回成功
        if (i > 0) return ResponseData.buildOk();
        return ResponseData.buildError();
    }

    @Override
    public ResponseData selectByReID(String ReId) {
        HashMap<String,Object> map = new HashMap<>();
        map.put("requisition_id",ReId);
        List<RequisitionHeader> acceptInformList = requisitionHeaderMapper.selectByMap(map);
        return ResponseData.buildOk(acceptInformList);
    }

    @Override
    public ResponseData selectByAccId(String acceptId) {
        HashMap<String,Object> map = new HashMap<>();
        map.put("accept_id",acceptId);
        map.put("status","申请中");
        List<RequisitionHeader> acceptInformList = requisitionHeaderMapper.selectByMap(map);
        return ResponseData.buildOk(acceptInformList);
    }
//需要级联删除
    @Override
    public ResponseData RemoveAdmin(String id) {
        RequisitionHeader requisitionHeader = requisitionHeaderMapper.selectById(id);
        //获取领料编号
        String RequisitionId =  requisitionHeader.getRequisitionId();

        QueryWrapper<MaterialRequisition> wrapper = new QueryWrapper<>();
        wrapper.eq("requisition_id",RequisitionId);

        //MaterialRequisition materialRequisition = materialRequisitionMapper.selectList();
        //materialRequisitionMapper.deleteById();
        if (requisitionHeader.getStatus().equals("申请中")) {
            //System.out.println("进入");
            int i = requisitionHeaderMapper.deleteById(id);
            int m = materialRequisitionMapper.delete(wrapper);
            if (i > 0 && m > 0) return ResponseData.buildOk();
            return ResponseData.buildError();
        }
        return ResponseData.buildError();
    }

    @Override
    public ResponseData editStatus(RequisitionHeader requisitionHeader) {

        String AccID = requisitionHeader.getAcceptId();
        QueryWrapper<AcceptGoods> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("accept_id",AccID);
        List<AcceptGoods> list =  acceptGoodsMapper.selectList(wrapper1);

        for (AcceptGoods ac: list
             ) {
            if(ac.getType().equals("特殊二")){
                requisitionHeader.setStatus("审核二");
                break;
            }
            else if(ac.getType().equals("特殊一")){
                requisitionHeader.setStatus("审核一");
            }
        }

        String ReID = requisitionHeader.getRequisitionId();
        QueryWrapper<MaterialRequisition> wrapper = new QueryWrapper<>();
        wrapper.eq("requisition_id",ReID);

        int n = materialRequisitionMapper.selectCount(wrapper);

        if (n > 0) {
            int i = requisitionHeaderMapper.updateById(requisitionHeader);
            if(i > 0){
                return ResponseData.buildOk();
            }
            //System.out.println("成功插入");
            return ResponseData.buildError();
        }
        return ResponseData.buildError("当前未申请货物");
    }

    @Override
    public ResponseData auditPassed(String requisitionId) {
        UpdateWrapper<RequisitionHeader> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("requisition_id", requisitionId).set("depart_leader", "通过");
        updateWrapper.eq("requisition_id", requisitionId).set("status", "完成");

        if (update(updateWrapper)) {
            System.out.println("部门审批成功1");
            return ResponseData.buildOk();
        }
        return ResponseData.buildError();
    }

    @Override
    public ResponseData departPassed(String requisitionId) {
        UpdateWrapper<RequisitionHeader> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("requisition_id", requisitionId).set("depart_leader", "通过");
        if (update(updateWrapper)) {
            System.out.println("部门审批成功2");
            return ResponseData.buildOk();
        }
        return ResponseData.buildError();
    }

    @Override
    public ResponseData auditFailed(String requisitionId) {
        UpdateWrapper<RequisitionHeader> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("requisition_id", requisitionId).set("status", "申请中");
        if (update(updateWrapper)) {
            System.out.println("部门审批未通过，退回审批中");
            return ResponseData.buildOk();
        }
        return ResponseData.buildError();
    }

    @Override
    public ResponseData unitPassed(String requisitionId) {
        UpdateWrapper<RequisitionHeader> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("requisition_id", requisitionId).set("company_leader", "通过");
        updateWrapper.eq("requisition_id", requisitionId).set("status", "完成");
        if (update(updateWrapper)) {
            System.out.println("单位审批成功2");
            return ResponseData.buildOk();
        }
        return ResponseData.buildError();
    }

    @Override
    public ResponseData unitFailed(String requisitionId) {
        UpdateWrapper<RequisitionHeader> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("requisition_id", requisitionId).set("status", "申请中")
                .set("depart_leader", null);
        if (update(updateWrapper)) {
            System.out.println("单位审批未通过，退回审批中");
            return ResponseData.buildOk();
        }
        return ResponseData.buildError();
    }

    @Override
    public ResponseData selectByStatus(String status) {
        QueryWrapper<RequisitionHeader> wrapper = new QueryWrapper<>();
        wrapper.eq("status", status);
        List<RequisitionHeader> requisitionHeaderList = requisitionHeaderMapper.selectList(wrapper);
        return ResponseData.buildOk(requisitionHeaderList.size(), requisitionHeaderList);
    }

    @Override
    public ResponseData deleteByRequisitionId(String requisitionId) {
        // 数据库设置级联删除
        QueryWrapper<RequisitionHeader> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(RequisitionHeader::getRequisitionId, requisitionId);
        if (remove(queryWrapper)) {
            return ResponseData.buildOk();
        }
        return ResponseData.buildError();
    }


}
